package com.inspur.resourcequery;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.inspur.common.BaseActivity;
import com.inspur.common.MyApplication;
import com.inspur.common.ScreenInfo;
import com.inspur.common.UserInfo;
import com.inspur.common.domain.RequestInfo;
import com.inspur.common.utils.URLManager;
import com.inspur.common.view.CustomDialog;
import com.inspur.common.view.timePicker.WheelMain;
import com.inspur.resourcequery.model.ResAttr;
import com.inspur.resourcequery.model.ResData;

public class AttributeModifyActivity extends BaseActivity {

	public ResData model;
	public ResAttr attribute;
	//属性名
	private TextView attributeNameTv;
	//属性值
	private EditText attributeValueEt;
	//备注
	private EditText remarkEdt;
	//提交按钮
	private Button resetBtn;
	//提交按钮
	private Button submitBtn;
	
	//屏幕大小获取管理类
	ScreenInfo screenInfo;
	//算定义时间管理控件
	WheelMain wheelMain;

	public ResAttr newAttr;
	private MyApplication application;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		application = (MyApplication)this.getApplication();
		
		model = (ResData) getIntent().getSerializableExtra("model");
		attribute = (ResAttr) getIntent().getSerializableExtra("attribute");
		actionBar.setTitle("属性修改");

		attributeNameTv.setText(attribute.getAttributeCnName());
		attributeValueEt.setText(attribute.getAttributeTransValue());
		screenInfo = new ScreenInfo(this);
		initAttributeListener();
		
	}

	/**
	 * 属性值有手填、日期选择、下拉选择、分页选择多种情况。需要根据不同的类型提供不同的输入。
	 */
	public void initAttributeListener(){
		Log.d("ifChange", attribute.getIfChange() + "");
		Log.d("dataType", attribute.getDataType());
		if(attribute.getIfChange() == 0){
			
			if(attribute.getDataType().equalsIgnoreCase("int") ||
					attribute.getDataType().equalsIgnoreCase("integer") ||
					attribute.getDataType().equalsIgnoreCase("double") ||
					attribute.getDataType().equalsIgnoreCase("float") ){
				
				attributeValueEt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
				
			} else if(attribute.getDataType().equalsIgnoreCase("date") ||
					attribute.getDataType().equalsIgnoreCase("datetosecond")){
				attributeValueEt.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						AttributeModifyActivity.this.showTimeDialog();
					}
				});
				attributeValueEt.setOnFocusChangeListener(new OnFocusChangeListener() {
					@Override
					public void onFocusChange(View arg0, boolean arg1) {
						if(arg1){
							attributeValueEt.clearFocus();
							AttributeModifyActivity.this.showTimeDialog();
						}
					}
				});
			}
			
		} else {
			
			attributeValueEt.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					turn2AttrSelect(AttributeListActivity.class);
				}
			});
			
			attributeValueEt.setOnFocusChangeListener(new OnFocusChangeListener() {
				@Override
				public void onFocusChange(View arg0, boolean arg1) {
					// TODO Auto-generated method stub
					if(arg1){
						turn2AttrSelect(AttributeListActivity.class);
					}
				}
			});
		}
		
	}

	public void turn2AttrSelect (Class<?> cls){
		
		Intent intent = new Intent(AttributeModifyActivity.this, cls);
		Bundle bundle = new Bundle();
		bundle.putSerializable("model", model);
		bundle.putSerializable("attribute", attribute);
		intent.putExtras(bundle);
		startActivityForResult(intent, 100);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 100){
			if(data != null && data.getSerializableExtra("newAttr") != null){
				
				newAttr = (ResAttr)data.getSerializableExtra("newAttr");
				attributeValueEt.setText(newAttr.getAttributeTransValue());
			}
		}
	}
	
	/**
	 * 弹出时间选择控件
	 */
	protected void showTimeDialog(){
		//自定义日期选择控件，类似iphone效果
		View timePickerView=LayoutInflater.from(AttributeModifyActivity.this).inflate(R.layout.timepicker, null);
		wheelMain = new WheelMain(timePickerView);
		wheelMain.screenheight = screenInfo.getHeight();
		wheelMain.initDateTimePicker(attributeValueEt.getText().toString());
		
		final CustomDialog dialog = new CustomDialog(AttributeModifyActivity.this, R.style.dialog, timePickerView);
		dialog.show();
		
		Button clearButton = (Button)timePickerView.findViewById(R.id.clear_btn);
		Button confirmButton = (Button)timePickerView.findViewById(R.id.confirm_btn);
		
		
		clearButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				attributeValueEt.setText("");
				dialog.dismiss();
			}
		});
		
		confirmButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				attributeValueEt.setText(wheelMain.getTime());
				dialog.dismiss();
			}
		});
	}
	
	/**
	 * 修改上报
	 */
	protected void modify() {
		
		if(attributeValueEt.getText().toString().equals(attribute.getAttributeTransValue())){
			Toast.makeText(this, "属性未做修改", Toast.LENGTH_SHORT).show();
		} else if(attributeValueEt.getText().toString().trim().equals("")){
			Toast.makeText(this, "请填写属性值", Toast.LENGTH_SHORT).show();
		} else {
			
			RequestInfo req = new RequestInfo();
			req.context = AttributeModifyActivity.this;
			req.requestUrl = URLManager.URL + URLManager.CHECK_ATTRIBUTE_MODIFY;
			
			try {
				JSONObject condition = new JSONObject();
				condition.put("ResClassName", model.getResclassenname());
				condition.put("ResClassNameCn", model.getResclasscnname() == null ? "" :  model.getResclasscnname());
				condition.put("name", model.getName());
				condition.put("creator", application.getUserInfo().getUseraccount());
				condition.put("content", remarkEdt.getText());
				condition.put("int_id", model.getInt_id());
				condition.put("attr_enname", attribute.getAttributeEnName());
				condition.put("attr_cnname", attribute.getAttributeCnName());
				condition.put("attr_value", attribute.getAttributeValue());
				condition.put("attr_trans_value", attribute.getAttributeTransValue());
				
				if(newAttr != null){
					condition.put("m_attr_value", newAttr.getAttributeValue());
					condition.put("m_attr_trans_value", newAttr.getAttributeTransValue());
				} else {
					condition.put("m_attr_value", attributeValueEt.getText());
					condition.put("m_attr_trans_value", attributeValueEt.getText());
				}
				
				req.params = condition.toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			super.getDataFromServer(req, new DataCallback<String>() {
				@Override
				public void processData(String paramObject) {
					Log.d("paramObject:", paramObject);
					if("0".equals(paramObject)){
						Toast.makeText(AttributeModifyActivity.this, "修改失败！", Toast.LENGTH_SHORT).show();
					} else if("1".equals(paramObject)){
						Toast.makeText(AttributeModifyActivity.this, "已提交审核！", Toast.LENGTH_SHORT).show();
//						Toast.makeText(AttributeModifyActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
						AttributeModifyActivity.this.setResult(10);
						AttributeModifyActivity.this.finish();
					} else {
						Toast.makeText(AttributeModifyActivity.this, "已提交审核！", Toast.LENGTH_SHORT).show();
//						Toast.makeText(AttributeModifyActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
						AttributeModifyActivity.this.setResult(20);
						AttributeModifyActivity.this.finish();
					}
				}
				
				@Override
				public void getDataFailed() {
					// 这里仅作演示，实际请求失败后怎么操作根据实际情况来看
					
				}
				
			}, AttributeModifyActivity.this);
		}
	}
	
	OnClickListener submitBtnClick = new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			modify();
		}
	};
	
	OnClickListener resetBtnClick = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			attributeValueEt.setText(attribute.getAttributeTransValue());
			newAttr = null;
		}
	};
	
	
	@Override
	protected void loadViewLayout() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_attribute_modify);
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		attributeNameTv = (TextView)this.findViewById(R.id.attibute_name_tv);
		attributeValueEt = (EditText)this.findViewById(R.id.attibute_name_et);
		remarkEdt = (EditText)this.findViewById(R.id.remark_et);
		submitBtn = (Button)this.findViewById(R.id.submit_btn);
		resetBtn = (Button)this.findViewById(R.id.reset_btn);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		submitBtn.setOnClickListener(submitBtnClick);
		resetBtn.setOnClickListener(resetBtnClick);
	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}
}
